import Common from "../Common";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Page extends Common {


    protected currentPage = 1;
    protected lastPage = 1;
    protected callback;


    @property(cc.Label)
    protected contentLabel: cc.Label

    protected start() {
        this.node.getChildByName('prev').on(cc.Node.EventType.TOUCH_START, this.onPrev, this)
        this.node.getChildByName('next').on(cc.Node.EventType.TOUCH_START, this.onNext, this)
    }

    init(currentPage: number = 1, lastPage: number = 1) {
        this.currentPage = currentPage;
        this.lastPage = lastPage;
        this.contentLabel.string = `${currentPage}/${lastPage}`
    }

    setCallback(callback?: any) {
        this.callback = callback;
    }

    protected onPrev() {
        this.onBtnClick()
        if (this.currentPage <= 1) {
            return;
        }
        this.currentPage--;
        if (typeof this.callback === 'function') {
            this.callback(this.currentPage)
        }
    }

    protected onNext() {
        this.onBtnClick()
        if (this.currentPage >= this.lastPage) {
            return;
        }
        this.currentPage++;
        if (typeof this.callback === 'function') {
            this.callback(this.currentPage)
        }
    }
}
